<?php
/*
 * @Author: lokei
 * @Date: 2022-09-19 13:45:10
 * @LastEditors: lokei
 * @LastEditTime: 2022-11-17 18:22:14
 * @Description: 
 */
namespace App\Http\Controllers\Topic;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Topic\TopicModel;
use Illuminate\Http\Request;

class TopicController extends Controller
{
	public function get(Request $request)
	{
		$topic = TopicModel::where('id', '=', $request->input('id'))->first();
		$res = ResultTool::success();
		$res['data'] = $topic;
		return $res;
	}
	public function list(Request $request)
	{
		$items = TopicModel::with(['comment' => function($query) {
			$query->select(['topic_id', 'user_info', 'content'])->limit(3);
		}])->get();
		$data['items'] = $items;
		$data['count'] = count($items);
		$res = ResultTool::success();
		$res['data'] = $data;
		return $res;
	}
	public function update(Request $request)
	{
		$status = '0';
		$topic = TopicModel::where('id', '=', $request->input('id'))->first();
		$topic->title = $request->input('title');
		$topic->img_display = $request->input('img_display');
		$topic->content = $request->input('content');
		$topic->status = $request->input('status');
		$topic->save();
		return ResultTool::success();
	}
	public function create(Request $request)
	{
		$topic = new TopicModel();
		$topic->title = $request->input('title');
		$topic->img_display = $request->input('img_display');
		$topic->content = $request->input('content');
		$topic->createtime = date('Y-m-d H:i:s', time());
		$topic->status = $request->input('status');
		$topic->save();
		return ResultTool::success();
	}

	public function release(Request $request) {
		$user = app('auth')->user();
		$topic = new TopicModel();
		$topic->uid = $user->uid;
		$topic->user_info = json_encode([
			'name' => $user->realname ? $user->realname : $user->nickname,
			'avatar' => $user->avatar
		]);
		$topic->content = $request->input('content');
		$topic->images = $request->input('images');
		$topic->created_at = date('Y-m-d H:i:s', time());
		$topic->status = '1';
		$topic->save();
		return ResultTool::success();
	}

	public function delete(Request $request)
	{
		TopicModel::where('id', '=', $request->input('id'))->delete();
		return ResultTool::success();
	}
}
